package ar.edu.unlu.sistemas.p2p.android;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class PreferencesActivity extends AbsP2PActivity implements OnClickListener {

	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.movirdroid_preferences);
        Button btn = (Button)findViewById(R.id.btn_save_preferences);
        btn.setOnClickListener(this);
        bindService(new Intent(PreferencesActivity.this,AppService.class), mConnection, BIND_AUTO_CREATE);
    }
    
    public void onServiceConnectedWithActivity(){
    	super.onServiceConnectedWithActivity();
    	EditText edt;
    	Map<String,String> prefMap;
    	prefMap = this.appService.loadPreferences();
    	edt = (EditText) this.findViewById(R.id.ed_txt_alias_preferences);
		edt.setText(prefMap.get("PEER_ALIAS"));
		edt = (EditText) this.findViewById(R.id.ed_txt_corpusdir_preferences);
		edt.setText(prefMap.get("CORPUS_DIR"));
		edt = (EditText) this.findViewById(R.id.ed_txt_iterations_preferences);
		edt.setText(prefMap.get("WAITING_TASK_ITERATIONS"));
		edt = (EditText) this.findViewById(R.id.ed_txt_port_preferences);
		edt.setText(prefMap.get("TCP_LISTEN_PORT"));
		edt = (EditText) this.findViewById(R.id.ed_txt_ttl_preferences);
		edt.setText(prefMap.get("MSG_TTL"));
		edt = (EditText) this.findViewById(R.id.ed_txt_threshold_preferences);
		edt.setText(prefMap.get("THRESHOLD"));
		CheckBox chk = (CheckBox) this.findViewById(R.id.chk_words_cache_preferences);
		chk.setChecked(new Boolean(prefMap.get("USE_WORDS_CACHE")).booleanValue());
		chk = (CheckBox) this.findViewById(R.id.chk_performed_query_process);
		chk.setChecked(new Boolean(prefMap.get("PROCESS_QUERY_PERFORMED")).booleanValue());
		chk = (CheckBox) this.findViewById(R.id.chk_boolean_extended);
		chk.setChecked(new Boolean(prefMap.get("BOOLEAN_EXTENDED_MODE")).booleanValue());
    }
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		EditText edt;
		Map<String,String> prefMap = new HashMap<String,String>();
		switch (v.getId()) {
		case R.id.btn_save_preferences:
			edt = (EditText) this.findViewById(R.id.ed_txt_alias_preferences);
			prefMap.put("PEER_ALIAS", edt.getText().toString());
			edt = (EditText) this.findViewById(R.id.ed_txt_corpusdir_preferences);
			prefMap.put("CORPUS_DIR", edt.getText().toString());
			edt = (EditText) this.findViewById(R.id.ed_txt_iterations_preferences);
			prefMap.put("WAITING_TASK_ITERATIONS", edt.getText().toString());
			edt = (EditText) this.findViewById(R.id.ed_txt_port_preferences);
			prefMap.put("TCP_LISTEN_PORT", edt.getText().toString());
			edt = (EditText) this.findViewById(R.id.ed_txt_ttl_preferences);
			prefMap.put("MSG_TTL", edt.getText().toString());
			edt = (EditText) this.findViewById(R.id.ed_txt_threshold_preferences);
			prefMap.put("THRESHOLD", edt.getText().toString());
			CheckBox chk = (CheckBox) this.findViewById(R.id.chk_words_cache_preferences);
			prefMap.put("USE_WORDS_CACHE", String.valueOf(chk.isChecked()));
			chk = (CheckBox) this.findViewById(R.id.chk_performed_query_process);
			prefMap.put("PROCESS_QUERY_PERFORMED", String.valueOf(chk.isChecked()));
			chk = (CheckBox) this.findViewById(R.id.chk_boolean_extended);
			prefMap.put("BOOLEAN_EXTENDED_MODE", String.valueOf(chk.isChecked()));
			this.appService.savePreferences(prefMap);
			this.onBackPressed();
			break;
		default:
			break;
		}
	}

}
